package service.Impl;

import dao.WorkPlanDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pojo.DoctorWork;
import pojo.WorkPlan;
import service.WorkPlanService;


import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;





@Service
public class WorkPlanServiceImpl implements WorkPlanService {
    private static final Logger logger = LoggerFactory.getLogger(WorkPlanServiceImpl.class);
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
    @Autowired
    private WorkPlanDao workPlanDao;

    @Override
    public WorkPlan getWorkPlanByDate(int year, int month, int day) {
        return workPlanDao.selectByDay(year, month, day);
    }

    @Override
    @Transactional
    public boolean updateWorkPlanStatus(String day, int status) {
        try {
            workPlanDao.updateByDay(day, status);
            return true;
        } catch (Exception e) {
            // 记录日志
            return false;
        }
    }

    @Override
    public List<DoctorWork> getWeeklyWorkPlan(String week) {
        return workPlanDao.getWorkPlanByWeek(week);
    }

    @Override
    @Transactional
    public boolean createWorkPlan(WorkPlan workPlan) {
        try {
            workPlanDao.insert(workPlan);
            return true;
        } catch (Exception e) {
            // 记录日志
            return false;
        }
    }
    @Override
    public List<WorkPlan> getWeekPlanByDoctor(List<String> days, String doctorId) {
        return workPlanDao.selectByDaysAndDoctor(days, doctorId);
    }
    // 新增：查询医生出诊详情的实现
    @Override
    public List<Map<String, Object>> selectWorkDetails() {
        return workPlanDao.selectWorkDetails();
    }
    @Override
    public List<Map<String, Object>> selectDoctorWorkById(String doctorId) {
        return workPlanDao.selectDoctorWorkById(doctorId);
    }

    @Override
    @Transactional
    public boolean insertWorkPlan(Map<String, Object> params) {
        try {
            workPlanDao.insertWorkPlan(params);
            return true;
        } catch (Exception e) {
            logger.error("插入医生排班失败: {}", e.getMessage(), e);
            return false;
        }
    }
    // 修改：删除方法实现变更
    @Override
    @Transactional
    public boolean deleteDoctorWork(String scheduleId) {
        try {
            workPlanDao.deleteByDoctorIdAndDay(scheduleId);
            return true;
        } catch (Exception e) {
            logger.error("删除医生排班失败: {}", e.getMessage(), e);
            return false;
        }
    }
    @Override
    @Transactional
    public boolean updateDoctorWork(Map<String, Object> params) {
        try {
            // 增加参数非空校验
            if (params.get("scheduleId") == null || params.get("doctorId") == null) {
                logger.error("更新医生排班失败：scheduleId和doctorId不能为空");
                return false;
            }
            // 只转换 scheduleId，doctorId 保持字符串
            if (params.get("scheduleId") instanceof String) {
                params.put("scheduleId", Integer.parseInt((String) params.get("scheduleId")));
            }
            // doctorId 不做类型转换
            workPlanDao.updateDoctorWork(params);
            return true;
        } catch (NumberFormatException e) {
            logger.error("更新医生排班失败：参数格式错误", e);
            return false;
        } catch (Exception e) {
            logger.error("更新医生排班失败: {}", e.getMessage(), e);
            return false;
        }
    }

    @Override
    public List<Map<String, Object>> getDoctorWorkByDayAndRoomId(String day, Integer roomId) {
        return workPlanDao.selectByDayAndRoomId(day, roomId);
    }
}


